package com.huilink.webmail.util.client.widgets;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.MouseListener;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.Widget;

public class HelpBox extends PopupPanel implements HasHTML, MouseListener {

  private HTML caption = new HTML();
  private HorizontalPanel cap = new HorizontalPanel();
  private Widget child;
  private boolean dragging;
  private int dragStartX, dragStartY;
  private FlexTable panel = new FlexTable();
  private ImageButton closeBtn = null;

//  public HelpBox() {
//    this(false);
//  }

  public HelpBox(boolean autoHide) {
    super(autoHide);
    cap.add(caption);
    panel.setWidget(0,0,cap);
    panel.setHeight("100%");
    panel.setBorderWidth(0);
    panel.setCellPadding(0);
    panel.setCellSpacing(0);
    panel.getCellFormatter().setHeight(1,0,"100%");
    panel.getCellFormatter().setWidth(1,0,"100%");
    panel.getCellFormatter().setAlignment(1,0, HasHorizontalAlignment.ALIGN_CENTER, HasVerticalAlignment.ALIGN_MIDDLE);
    super.setWidget(panel);

    setStyleName("gwt-HelpBox");
    cap.setStyleName("Caption");
    caption.setStyleName("Text");
    caption.addMouseListener(this);
  }
  
  public HelpBox(String styleBox, String styleCaption, String styleCaptionText) {
      this(false);
      if (styleBox!=null) {
          setStyleName(styleBox);
      }
      if (styleCaption!=null) {
          cap.setStyleName(styleCaption);
      }
      if (styleCaptionText!=null) {
          caption.setStyleName(styleCaptionText);
      }
  }

  public String getHTML() {
    return caption.getHTML();
  }

  public String getText() {
    return caption.getText();
  }

  public void onMouseDown(Widget sender, int x, int y) {
    dragging = true;
    DOM.setCapture(caption.getElement());
    dragStartX = x;
    dragStartY = y;
  }

  public void onMouseEnter(Widget sender) {
  }

  public void onMouseLeave(Widget sender) {
  }

  public void onMouseMove(Widget sender, int x, int y) {
    if (dragging) {
      int absX = x + getAbsoluteLeft();
      int absY = y + getAbsoluteTop();
      setPopupPosition(absX - dragStartX, absY - dragStartY);
    }
  }

  public void onMouseUp(Widget sender, int x, int y) {
    dragging = false;
    DOM.releaseCapture(caption.getElement());
  }

  public boolean remove(Widget w) {
    if (child != w) {
      return false;
    }

    panel.remove(w);
    return true;
  }

  public void setHTML(String html) {
    caption.setHTML(html);
  }

  public void setText(String text) {
    caption.setText(text);
  }

  public void setWidget(Widget w) {
    // If there is already a widget, remove it.
    if (child != null) {
      panel.remove(child);
    }

    // Add the widget to the center of the cell.
    if (w != null) {
      panel.setWidget(1,0,w);
    }
 
    child = w;
  }
  
  public void setCloseButton(ImageButton imgBtn) {
	  this.closeBtn = imgBtn;
	  closeBtn.addClickListener(new ClickListener() {
		public void onClick(Widget sender) {
			hide();
		}
	  });
	  cap.add(closeBtn);
	  cap.setCellHorizontalAlignment(closeBtn, HasHorizontalAlignment.ALIGN_RIGHT);
  }
}
